﻿using System;
using System.Drawing;
using Microsoft.Samples.DslTools.ConfigurationSectionDesigner.Properties;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Microsoft.Samples.DslTools.ConfigurationSectionDesigner
{
    public partial class ConfigurationElementShapeBase
    {
        #region Provide Icons For Compartment Entries

        protected override CompartmentMapping[] GetCompartmentMappings(Type melType)
        {
            // The ConfigurationElementShapeBase is defined as Double Derived, so we
            // can override this method and modify the return value to include an icon image getter.

            // First we retrieve the basic compartment mappings as configured in the DSL definition.
            CompartmentMapping[] mappings = base.GetCompartmentMappings(melType);

            // Then, for each compartment, we provide an image getter that determines the icon to display.
            foreach (ElementListCompartmentMapping mapping in mappings)
            {
                mapping.ImageGetter = CompartmentImageProvider;
            }
            return mappings;
        }

        /// <summary>
        /// Determines the icon to show in a compartment entry, based on its properties.
        /// </summary>
        /// <param name="element">The configuration property being shown in the compartment.</param>
        /// <returns>The icon to use to represent the configuration property.</returns>
        private Image CompartmentImageProvider(ModelElement element)
        {
            // Show icons for the most interesting cases (key and required properties).
            ConfigurationProperty prop = (ConfigurationProperty)element;
            if (prop.IsKey)
            {
                return Resources.KeyIcon;
            }
            else if (prop.IsRequired)
            {
                return Resources.RequiredIcon;
            }
            else
            {
                return null;
            }
        }

        #endregion
    }
}